package com.jiyun.service.imp;

import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.dao.UserDao;
import com.jiyun.pojo.result.ResultVo;
import com.jiyun.pojo.user.User;
import com.jiyun.pojo.user.UserVo;
import com.jiyun.service.UserService;
import com.jiyun.uitl.AppJwtUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件名:UserServiceImp
 * 创建者: 闫俊涛
 * 创建时间:2022/6/24 0:27
 * 描述:这是一个实例
 */
@Service
public class UserServiceImp extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private  UserDao userDao;

    @Autowired
    private RabbitTemplate rabbitTemplate;



    @Override
    public ResultVo sendCode(String phone) {
     /*  QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("phone",phone);
       long cloud= baseMapper.selectCount(query);*/
        long cloud = 1;
        if (cloud>0){
            rabbitTemplate.convertAndSend("java.driect.exchange",
                    "good",phone);
            return new ResultVo(200,"短信发送成功",null);
        }else {

            return new ResultVo(300,"手机号未注册",null);
        }
    }

    @Override
    public ResultVo smsLogin (UserVo userVo) {
        String  rediscode = stringRedisTemplate.boundValueOps(userVo.getPhone()).get();


        // 判断用户输入的手机号是否正确
        // 将验证码保存到redis中

        if (rediscode!=null&& rediscode!=""){
            // redis中的验证码没有过期
            if (rediscode.equals(userVo.getCode())){
                //redis 中验证码和用户输入的验证码一致
                Wrapper wrapper = new QueryWrapper<User>();
                ((QueryWrapper) wrapper).eq("phone", userVo.getPhone());
                List<User> list = list(wrapper);
                User adUser = list.get(0);
              if (list != null && list.size() == 1){
                 QueryWrapper<User> queryWrapper = new QueryWrapper<>();
                 queryWrapper.eq("phone", userVo.getPhone());
                  User user = userDao.selectOne(queryWrapper);
                  Integer id = user.getId();
                  Map<String, Object> map = new HashMap<>();
                  map.put("token", AppJwtUtil.getToken(id.longValue()));
                  map.put("id",id);
                  map.put("user", adUser);
                  return  new  ResultVo(200,"成功",map);
              }
            }else {
                return new ResultVo(301,"验证码错误",null);
            }
        }else {
            return new ResultVo(300,"验证码超期,请重新获取",null);
        }

        return  new ResultVo(302,"手机不存在",null);

    }




}
